package net.mingsoft.examine.biz.impl;

import net.mingsoft.base.biz.impl.BaseBizImpl;
import net.mingsoft.base.dao.IBaseDao;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.core.SessionContext;
import net.mingsoft.cms.biz.IContentBiz;
import net.mingsoft.cms.entity.ContentEntity;
import net.mingsoft.cms.entity.ContentResult;
import net.mingsoft.examine.biz.ExamineBiz;
import net.mingsoft.examine.dao.ExamineDao;
import net.mingsoft.examine.entity.ExamineEntity;
import net.mingsoft.examine.entity.QueryExamineVo;
import net.mingsoft.examine.enums.NoticeEnum;
import net.mingsoft.examine.utils.BlankUtil;
import net.mingsoft.examine.utils.DateUtil;
import net.mingsoft.message_center.action.r.MessageResponse;
import net.mingsoft.message_center.biz.MessageBiz;
import net.mingsoft.message_center.entity.MessageCenterEntity;
import net.mingsoft.message_center.enums.MessageType;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("cmsExamineBizImpl")
public class ExamineBizImpl extends BaseBizImpl<ExamineDao, ExamineEntity> implements ExamineBiz {
    @Autowired
    private ExamineDao examineDao;

    @Resource
     private  MessageBiz messageBiz;

    @Resource
    private IContentBiz contentBiz;

    @Override
    protected IBaseDao getDao() {
        // TODO Auto-generated method stub
        return examineDao;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public ResultData fbsq(ExamineEntity examineEntity) {
        ContentResult contentResult=contentBiz.get(examineEntity.getReportId());
        if(!BlankUtil.isBlank(contentResult)){
            if(!BlankUtil.isBlank(contentResult.getApplyId())){
                examineEntity.setId(contentResult.getApplyId());
            }
        }
        examineEntity.setDel(0);
        examineEntity.setCreateBy(SessionContext.session().getIntegerId().toString());
        examineEntity.setCreatedUserName(SessionContext.session().getManagerName());
        examineEntity.setCreateDate(DateUtil.getNowDay());
        examineEntity.setStatus(1);
        boolean b=this.saveOrUpdate(examineEntity);
        ContentEntity contentEntity=new ContentEntity();
        ExamineEntity examineEntity1=this.getReportId(examineEntity.getReportId());
        if(!BlankUtil.isBlank(examineEntity1)){
            if(!BlankUtil.isBlank(examineEntity1.getId())){
                contentEntity.setApplyId(examineEntity1.getId());
            }
        }
        contentEntity.setId(examineEntity.getReportId().toString());
        //状态为待审批
        contentEntity.setSendApply("1");
        contentBiz.saveOrUpdate(contentEntity);
        if(!BlankUtil.isBlank(examineEntity.getNotice()) && examineEntity.getNotice().equals(1) && !BlankUtil.isBlank(examineEntity.getNoticeType())){
            //系统通知
            if(examineEntity.getNoticeType().contains(NoticeEnum.XTTZ.getCode().toString())){
                MessageCenterEntity messageResponse= new MessageCenterEntity();
                messageResponse.setFileOrReportName(examineEntity.getReportName());
                messageResponse.setMessageContent("您有一条待处理审批事项!");
                messageResponse.setMessageType(MessageType.PUB_REQUEST.getType());
                messageResponse.setOperateName(examineEntity.getCreatedUserName());
                messageResponse.setCreatedBy(SessionContext.session().getIntegerId().toString());
                messageResponse.setOperatorId(Long.parseLong(messageResponse.getCreatedBy()));
                messageResponse.setReceivedDate(DateUtil.dateToString(DateUtil.getNowDay(),DateUtil.FORMAT_ONE));
                messageResponse.setCreatedDate(DateUtil.dateToString(DateUtil.getNowDay(),DateUtil.FORMAT_ONE));
                messageBiz.add(messageResponse);
            }
            //短信通知
            else if(examineEntity.getNoticeType().contains(NoticeEnum.DX.getCode().toString())){

            }
            //邮箱通知
            else if(examineEntity.getNoticeType().contains(NoticeEnum.YX.getCode().toString())){

            }
        }
        if(b){
            return ResultData.build().success("申请成功！");
        }else{
            return ResultData.build().success("申请失败！");
        }

    }

    @Override
    public List<ExamineEntity> getList(QueryExamineVo queryExamineVo) {
        //我的申请
        if(queryExamineVo.getQuery().equals(1)){
            String createId=SessionContext.session().getIntegerId().toString();
            System.out.println("aaaaaaa"+createId);
            return examineDao.getWdsq(queryExamineVo,createId);
        }
        //审批列表
        else if(queryExamineVo.getQuery().equals(2)){
            return examineDao.getSplb(queryExamineVo);
        }
        else {
            return null;
        }
    }

    @Override
    public ExamineEntity getDetail(String id) {
        return this.getById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public boolean updateExamine(ExamineEntity examineEntity) {
        ContentEntity contentEntity=new ContentEntity();
        ExamineEntity examineEntity1=this.getById(examineEntity.getId());
        if(!BlankUtil.isBlank(examineEntity1)){
            if(!BlankUtil.isBlank(examineEntity1.getReportId())) {
                contentEntity.setId(examineEntity1.getReportId().toString());
            }
        }
        //状态与审批表一致
        contentEntity.setSendApply(examineEntity.getStatus().toString());
        contentBiz.saveOrUpdate(contentEntity);
        return this.saveOrUpdate(examineEntity);
    }

    @Override
    public ExamineEntity getReportId(String reportId) {
        return examineDao.getReportId(reportId);
    }
}
